package com.example.sxt_practise.mydate;

import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * @author gaofeng
 * @date 2022年09月08日 14:08
 */
public class TestData {
    public static void main(String[] args) {

        LocalDate today = LocalDate.now() ;
        System.out.println(today);
        System.out.println("year:" + today.getYear());
        // 英文 月
        System.out.println("month:" + today.getMonth());
        // 阿拉伯数字 月
        System.out.println("month:" + today.getMonthValue());
        System.out.println("day:" + today.getDayOfMonth());
        // 根据指定的年月日(必须有效)返回LocalDate类型的日期,
        LocalDate of = LocalDate.of(2018, 1, 1);
        System.out.println(of);

        MonthDay monthDay  = MonthDay.of(of.getMonthValue(), of.getDayOfMonth()) ;
        // 该函数返回本地日期，而不是null
        MonthDay day = MonthDay.from(of);
        System.out.println(monthDay + "=====" + day);
        if (monthDay.equals(day)) {
            System.out.println("是你的生日");
        }


        /********************************************/
        LocalTime now = LocalTime.now();
        System.out.println(now);
        LocalTime localTime = now.plusMinutes(5);
        System.out.println(localTime);
        LocalTime plus = now.plus(10, ChronoUnit.SECONDS);
        System.out.println(plus);
        // 一周后的日期
        LocalDate plus1 = of.plus(1, ChronoUnit.WEEKS);
    }

    public static LocalDate date2LocalDate(Date paramDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(paramDate);
        return LocalDate.of(cal.get(1), cal.get(2) + 1, cal.get(5));
    }

    private Map<Integer, Date[]> getWeeks(Date dbegin, Date dend) {
        if(Objects.isNull(dbegin) || Objects.isNull(dend) || dbegin.compareTo(dend) >= 0) return null;
        Date firstEndWeekDate = getEndDays4Week(dbegin);
        Map<Integer, Date[]> map = new HashMap<>();
        Date[] dateArr = new Date[]{dbegin, firstEndWeekDate};
        int index = 1; map.put(index, dateArr);
        for(Date start = firstEndWeekDate ; dend.compareTo(start) >= 0 ; ) {
            index++;
            LocalDate weekBegin = date2LocalDate(start).plusDays(1L);
            LocalDate weekEnd = date2LocalDate(start).plusWeeks(1L);
            Date weekEndDate = LocalDate2Date(weekEnd);
            start = weekEndDate;
            if(start.compareTo(dend) >= 0) {
                map.put(index, new Date[]{LocalDate2Date(weekBegin), dend});
                break;
            }
            map.put(index, new Date[]{LocalDate2Date(weekBegin), weekEndDate});
        }
        return map;
    }

    public static Date getEndDays4Week(Date dweek) {
        LocalDate cLocalDate = date2LocalDate(dweek);
        LocalDate dweekend = cLocalDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).minusDays(1L);
        return LocalDate2Date(dweekend);
    }

    public static Date LocalDate2Date(LocalDate localDate) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
        return Date.from(zdt.toInstant());
    }
}
